<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Member_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function kirim_ulang_password($phone) {
        $len = 6;
        $chars = '0123456789';
        $string = '';

        for ($i = 0; $i < $len; $i++) {
            $pos = rand(0, strlen($chars) - 1);
            $string .= $chars{$pos};
        }
        $string = trim($string);
        $sandi = trim(md5($string));

        $id_member = "";
        $query = $this->db->query("select id_member from member where no_hp =?", array($phone));
        $result = $query->result();
        foreach ($result as $data) {
            $id_member = $data->id_member;
        }

        if ($id_member != "") {
            $query = "insert into message_outbox (receiver,content,date_created,id_member,is_sent) values (?,?,now(),?,0)";
            $this->db->query($query, array($phone, "Password anda berhasil direset pada " . date("d-M-Y H:i:s") . ". Kata sandi anda  " . $string, $id_member));

            $this->db->query("update member set sandi =? where id_member=?", array($sandi, $id_member));

            return true;
        }
        return false;
    }

    function is_member_exist($phone) {
        $query = $this->db->query("select nama from member where no_hp =?", array($phone));
        $result = $query->result();
        foreach ($result as $data) {
            return $data->nama;
        }
        return "";
    }

    function daftar_baru($name, $phone, $email) {
        $len = 6;
        $chars = '0123456789';
        $string = '';

        for ($i = 0; $i < $len; $i++) {
            $pos = rand(0, strlen($chars) - 1);
            $string .= $chars{$pos};
        }
        $string = trim($string);
        $sandi = trim(md5($string));

        $data = array(
            mysql_real_escape_string($name),
            mysql_real_escape_string($phone),
            mysql_real_escape_string($email),
            $sandi);
        $query = "insert into member (nama,no_hp,email,sandi,join_date,date_created) values (?,?,?,?,now(),now())";
        $this->db->query($query, $data);
        $id_member = $this->db->insert_id();

        $data = array(
            ($id_member),
            ($string),
            mysql_real_escape_string(0));
        $query = "insert into sandi_sms (id_member,kata_sandi,is_sent,date_created) values (?,?,?,now())";
        $this->db->query($query, $data);

        $query = "insert into message_outbox (receiver,content,date_created,id_member,is_sent) values (?,?,now(),?,0)";
        $this->db->query($query, array($phone, "Selamat bergabung di belanjasms.com. Kata sandi anda: " . $string, $id_member));
    }

    function cek_login($phone, $sandi) {
        $data = array(
            md5(mysql_real_escape_string($sandi)),
            mysql_real_escape_string($phone));

        $sql = "select * from member where sandi=? and no_hp=?";
        $query = $this->db->query($sql, $data);

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $this->session->set_userdata(array(
                    "id_member" => $row->id_member,
                    "nama" => $row->nama,
                    "email" => $row->email,
                    "no_hp" => $row->no_hp
                ));

                $sql = "select * from kepemilikan_port where id_member=?";
                $query2 = $this->db->query($sql, array($row->id_member));
                $data_port = "";
                foreach ($query2->result() as $row2) {
                    if ($data_port != "") {
                        $data_port .= "," . $row2->port;
                    } else {
                        $data_port = $row2->port;
                    }
                }
                
                $this->session->set_userdata(array(
                    "data_port" => $data_port
                ));
                return true;
            }
            return true;
        }
        return false;
    }

    function getSaldoTerkini() {
        $query = "select * from member where id_member=" . $this->session->userdata('id_member');
        $result = mysql_query($query);
        $row = mysql_fetch_assoc($result);
        return $row['saldo'];
    }

    function updateSaldo($terpakai) {
        $saldo = $this->getSaldoTerkini();
        $data = array("saldo" => ($saldo - $terpakai));
        $this->db->where('id_member', $this->session->userdata('id_member'));
        $this->db->update('member', $data);
    }

    function updatePassword($password) {
        $id_member = $this->session->userdata('id_member');
        $phone = $this->session->userdata('no_hp');
        $sandi = trim(md5($password));

        $data = array("sandi" => $sandi);
        $this->db->where('id_member', $id_member);
        $this->db->update('member', $data);

        $data = array(
            ($id_member),
            ($sandi),
            mysql_real_escape_string(0));
//        $query = "insert into sandi_sms (id_member,kata_sandi,is_sent,date_created) values (?,?,?,now())";
//        $this->db->query($query, $data);

        $query = "insert into message_outbox (receiver,content,date_created,id_member,is_sent) values (?,?,now(),?,0)";
        $this->db->query($query, array($phone, "Sandi anda di belanjasms.com telah dirubah , Kata sandi anda yang baru: " . $password, $id_member));
    }
    
    
    
    function get_kepemilikan_port(){
        return $this->session->userdata("data_port");
    }
    
    function get_pesan_masuk($interval = "") {
        $base_query = "select k.*,m.nama_kontak,m.group_kontak from komplain k left join kontak_member m on k.sender = m.notelp_kontak AND m.id_member_owner = ? where ";
        if ($interval == ""){
            $query = $base_query." upper(k.isi) <> 'CEK' and k.receiver in (select port from kepemilikan_port where id_member = ?) and k.date_created = curdate() order by k.id_komplain desc";
        } else {
            $query = $base_query." upper(k.isi) <> 'CEK' and k.receiver in (select port from kepemilikan_port where id_member = ?) and k.date_created = DATE_FORMAT(date_sub(now(), interval ".$interval." day), '%Y-%m-%d') order by id_komplain desc";
        }        
        $row = $this->db->query($query,array($this->session->userdata('id_member'),$this->session->userdata('id_member')));
        return $row->result() ;
    }
}

?>
